package com.bbg.amyam.process.email;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;

@Component
public class NotificationServiceImpl implements NotificationService {

	@Autowired
	private transient JavaMailSender mailSender;

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	@Autowired
	private transient SimpleMailMessage templateMessage;

	@Autowired
	private VelocityEngine velocityEngine;

	public void sendTextMessage(String mailTo, String message) {
		org.springframework.mail.SimpleMailMessage mailMessage = new org.springframework.mail.SimpleMailMessage(
				templateMessage);
		mailMessage.setTo(mailTo);
		mailMessage.setText(message);
		mailSender.send(mailMessage);
	}

	public void sendEmail(final String toEmailAddresses,
			final String fromEmailAddress, final String subject,final Map<String,Object> module, String templateFileName) {

		sendEmail(toEmailAddresses, fromEmailAddress, subject, null, null, module, templateFileName);

	}

	public void sendEmailWithAttachment(final String toEmailAddresses,
			final String fromEmailAddress, final String subject,
			final String attachmentPath, final String attachmentName, final Map<String,Object> module, String templateFileName) {

		sendEmail(toEmailAddresses, fromEmailAddress, subject, attachmentPath,
				attachmentName,module, templateFileName);

	}

	private void sendEmail(final String toEmailAddresses,
			final String fromEmailAddress, final String subject,
			final String attachmentPath, final String attachmentName, final Map<String,Object> module,final String templateFileName) {

		MimeMessagePreparator preparator = new MimeMessagePreparator() {

			public void prepare(MimeMessage mimeMessage) throws Exception {

				
				MimeMessageHelper message = null;
				if (!StringUtils.isBlank(attachmentPath)) {
				message = new MimeMessageHelper(mimeMessage,
						true);
				}else{
					message = new MimeMessageHelper(mimeMessage);
				}
				message.setTo(toEmailAddresses);
				message.setFrom(new InternetAddress(fromEmailAddress));
				message.setSubject(subject);

				String body = VelocityEngineUtils.mergeTemplateIntoString(
						velocityEngine, templateFileName, "UTF-8", module);
				message.setText(body, true);

				if (!StringUtils.isBlank(attachmentPath)) {
					FileSystemResource file = new FileSystemResource(
							attachmentPath);
					message.addAttachment(attachmentName, file);
				}
			}
		};
		
		this.mailSender.send(preparator);
	}
}
